package pers.zc.activiti.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import pers.zc.activiti.config.AsyncExecutorConfig;
import pers.zc.activiti.service.AsyncService;
import pers.zc.activiti.service.impl.AsyncTestDefaultClass;
import pers.zc.activiti.thread.AsyncClas;
import pers.zc.activiti.thread.CurrentHashMapTest;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 * @Annotion:
 * @ClassName: AsyncController
 * @Author: Lin Fuyuan
 * @Date: 2019/10/11  14:17
 * @Version: 1.0
 */
@Controller
@RequestMapping("/async")
public class AsyncController {

    @Autowired
    private AsyncService asyncService;
    @Autowired
    private AsyncExecutorConfig asyncExecutorConfig;
    @Autowired
    private CurrentHashMapTest currentHashMapTest;
    @Autowired
    private AsyncClas asyncClas;

    @GetMapping("test1")
    public void test() throws ExecutionException, InterruptedException {
        long time1 = System.currentTimeMillis();
        AsyncTestDefaultClass asyncTestDefaultClass = new AsyncTestDefaultClass();
        /*获取线程池*/
        ThreadPoolTaskExecutor asyncExecutor = (ThreadPoolTaskExecutor) asyncExecutorConfig.getAsyncExecutor();
//        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();

        Thread thread = new Thread(new Runnable() {
                @Override
            public void run() {
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("************"+Thread.currentThread().getName());
            }
        });
//        asyncExecutor.execute(thread);
//        asyncExecutor.execute(thread);
//        asyncExecutor.execute(thread);
//        asyncExecutor.execute(thread);
//        asyncExecutor.execute(thread);
//        asyncExecutor.execute(thread);
        asyncExecutor.submit(thread);
        asyncExecutor.submit(thread);
        asyncExecutor.submit(thread);
        asyncExecutor.submit(thread);
        asyncExecutor.submit(thread);
//          new TestServiceImpl().startTest("123");
//        Future<String> s1 = asyncService.run();
        Future<String> s2 = asyncService.start1();
//        Future<String> s3 = asyncService.start2();
//        Future<String> s4 = asyncService.start3();
//        Future<String> s5 = asyncService.start3();
//        Future<String> s6 = asyncService.start3();
//        Future<String> s7 = asyncService.start3();
//        Future<String> s8 = asyncService.start3();
//        Future<String> s9 = asyncService.start3();
//        System.out.println(s1.get());
//        System.out.println(s2.get());
//        System.out.println(s3.get());
//        System.out.println(s4.get());
        long time2 = System.currentTimeMillis();
        System.out.println("用时:"+(time2-time1));
    }
    @GetMapping("/testMap")
    public void testCurrentHashMap(){
        ThreadPoolTaskExecutor asyncExecutor = (ThreadPoolTaskExecutor) asyncExecutorConfig.getAsyncExecutor();
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                for (int i = 0 ;i<100;i++){
                    AsyncClas.test(i);
                }
            }
        };

        System.out.println("100并发结束调用");
        asyncExecutor.execute(runnable);
    }

    @GetMapping("/testR")
    public void testAsyncRunnable(){
        asyncService.start1();
        asyncService.start2();
//        RequestAttributes requestAttributes = RequestContextHolder
//                .getRequestAttributes();
//        RequestContextHolder.setRequestAttributes(requestAttributes, true);
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
//                AsyncService asyncService = SpringContextUtils.getBean(AsyncService.class);
                //将RequestAttributes对象设置为子线程共享
//                ServletRequestAttributes sra = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
//                if (sra == null) {
//                    sra = new ServletRequestAttributes(new MyRequest());
//                }
//                sra.setAttribute("hubDataExtractTaskJobHandler", "", 0);

                asyncService.start1();
                asyncService.start2();
            }
        });
        thread.start();
    }
}
